package com.bss.controller;

import java.io.IOException;

import javax.faces.model.DataModel;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.andy.basics.Service;
import com.bss.model.Admission;
import com.bss.service.IdGenerationService;
import com.bss.service.UpdateAdmissionService;

@Controller
@Scope("request")
public class UpdateAdmissionController extends com.andy.basics.Controller<Admission> {

	private static final Logger LOGGER = Logger.getLogger(UpdateAdmissionController.class.getName());

	@Autowired
	private UpdateAdmissionService updateAdmissionService;

	@Autowired
	private IdGenerationService idGenerationService;

	@Override
	public Admission create() {
		return new Admission();
	}

	@Override
	public Service<Admission> getService() {
		return this.updateAdmissionService;
	}

	public DataModel admissions(final Object patientName) {
		if (patientName == null) {
			return null;
		}
		return this.idGenerationService.getCurrentAdmissionData(patientName.toString());
	}

	@Override
	public String select() {
		Admission admission = (Admission) this.idGenerationService.getData().getRowData();
		this.updateAdmissionService.selectAdmission(admission);
		return null;
	}

	@Override
	public String update() {
		this.updateAdmissionService.update();
		return null;
	}

	public int getUtensilDeposit() {
		return this.updateAdmissionService.getUtensilDeposit();
	}

	public void setUtensilDeposit(int utensilDeposit) {
		this.updateAdmissionService.setUtensilDeposit(utensilDeposit);
	}

	public void updateUtensilDeposit() {
		this.updateAdmissionService.setUtensilDeposit(this.getModel().getMaterialSet() == null ? 0 : this.getModel()
				.getMaterialSet().getDeposit());
	}

	@Override
	public String cancel() {
		try {
			this.setModel(null);
			this.getHttpUtil().redirect("admissionUpdate.jsf");
		} catch (IOException e) {
			UpdateAdmissionController.LOGGER.error(e.getMessage(), e);
		}
		return null;
	}

}
